home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / artdisplay / LocalCoverArtSearchGIO.py < prev    next >
Encoding:
Python Source  |  2009-04-07  |  6.3 KB  |  175 lines

  1. # -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*- 
  2. #
  3. # Copyright (C) 2006 - Ed Catmur <ed@catmur.co.uk>
  4. # Copyright (C) 2009 - Jonathan Matthew <jonathan@d14n.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2, or (at your option)
  9. # any later version.
  10. #
  11. # The Rhythmbox authors hereby grant permission for non-GPL compatible
  12. # GStreamer plugins to be used and distributed together with GStreamer
  13. # and Rhythmbox. This permission is above and beyond the permissions granted
  14. # by the GPL license by which Rhythmbox is covered. If you modify this code
  15. # you may extend this exception to your version of the code, but you are not
  16. # obligated to do so. If you do not wish to do so, delete this exception
  17. # statement from your version.
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
  26.  
  27. import os
  28. import rhythmdb
  29. import rb
  30. import gobject
  31. import gio
  32.  
  33. IMAGE_NAMES = ["cover", "album", "albumart", ".folder", "folder"]
  34. ITEMS_PER_NOTIFICATION = 10
  35. ART_SAVE_NAME = 'Cover.jpg'
  36. ART_SAVE_FORMAT = 'jpeg'
  37. ART_SAVE_SETTINGS = {"quality": "100"}
  38.  
  39. def file_root (f_name):
  40.     return os.path.splitext (f_name)[0].lower ()
  41.  
  42. def shared_prefix_length (a, b):
  43.     l = 0
  44.     while a[l] == b[l]:
  45.         l = l+1
  46.     return l
  47.  
  48.  
  49. class LocalCoverArtSearch:
  50.     def __init__ (self):
  51.         pass
  52.  
  53.     def _enum_dir_cb(self, fileenum, result, (results, on_search_completed_cb, entry, args)):
  54.         try:
  55.             files = fileenum.next_files_finish(result)
  56.             if files is None or len(files) == 0:
  57.                 print "okay, done; got %d files" % len(results)
  58.                 on_search_completed_cb(self, entry, results, *args)
  59.                 return
  60.  
  61.             for f in files:
  62.                 ct = f.get_attribute_string("standard::fast-content-type")
  63.                 if ct.startswith("image/") and f.get_attribute_boolean("access::can-read"):
  64.                     results.append(f.get_name())    # hm
  65.  
  66.             fileenum.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data=(results, on_search_completed_cb, entry, args))
  67.         except Exception, e:
  68.             print "okay, probably done: %s" % e
  69.             on_search_completed_cb(self, entry, results, *args)
  70.  
  71.     def search (self, db, entry, on_search_completed_cb, *args):
  72.  
  73.         self.file = gio.File(entry.get_playback_uri())
  74.         if self.file.get_uri_scheme() in ('http','cdda'):
  75.             print 'not searching for local art for %s' % (self.file.get_uri())
  76.             on_search_completed_cb (self, entry, [], *args)
  77.             return
  78.  
  79.         self.artist = db.entry_get (entry, rhythmdb.PROP_ARTIST)
  80.         self.album = db.entry_get (entry, rhythmdb.PROP_ALBUM)
  81.  
  82.         print 'searching for local art for %s' % (self.file.get_uri())
  83.         parent = self.file.get_parent()
  84.         enumfiles = parent.enumerate_children(attributes="standard::fast-content-type,access::can-read,standard::name")
  85.         enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data=([], on_search_completed_cb, entry, args))
  86.  
  87.     def search_next (self):
  88.         return False
  89.  
  90.     def get_best_match_urls (self, results):
  91.         parent = self.file.get_parent()
  92.  
  93.         # Compare lower case, without file extension
  94.         for name in [file_root (self.file.get_basename())] + IMAGE_NAMES:
  95.             for f_name in results:
  96.                 if file_root (f_name) == name:
  97.                     yield parent.resolve_relative_path(f_name).get_uri()
  98.  
  99.         # look for file names containing the artist and album (case-insensitive)
  100.         # (mostly for jamendo downloads)
  101.         artist = self.artist.lower()
  102.         album = self.album.lower()
  103.         for f_name in results:
  104.             f_root = file_root (f_name).lower()
  105.             if f_root.find (artist) != -1 and f_root.find (album) != -1:
  106.                 yield parent.resolve_relative_path(f_name).get_uri()
  107.  
  108.         # if that didn't work, look for the longest shared prefix
  109.         # only accept matches longer than 2 to avoid weird false positives
  110.         match = (2, None)
  111.         for f_name in results:
  112.             pl = shared_prefix_length(f_name, self.file.get_basename())
  113.             if pl > match[0]:
  114.                 match = (pl, f_name)
  115.  
  116.         if match[1] is not None:
  117.             yield parent.resolve_relative_path(match[1]).get_uri()
  118.  
  119.     def pixbuf_save (self, plexer, pixbuf, uri):
  120.         def pixbuf_cb(buf):
  121.             f = gio.File(uri)
  122.             f.replace_contents_async(buf, plexer.send())
  123.             yield None
  124.             _, (file, result) = plexer.receive()
  125.             try:
  126.                 file.replace_contents_finish(result)
  127.             except Exception, e:
  128.                 print "error creating \"%s\": %s" % (uri, e)
  129.  
  130.         pixbuf.save_to_callback(pixbuf_cb, ART_SAVE_FORMAT, ART_SAVE_SETTINGS)
  131.  
  132.     def _save_dir_cb (self, enum, result, (db, entry, dir, pixbuf)):
  133.         artist, album = [db.entry_get (entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
  134.         try:
  135.             files = enum.next_files_finish(result)
  136.             if len(files) == 0:
  137.                 art_file = dir.resolve_relative_path(file.get_display_name())
  138.  
  139.             for f in files:
  140.                 ct = f.get_attribute_string("standard::fast-content-type")
  141.                 if ct.startswith("image/") or ct.startswith("x-directory/"):
  142.                     continue
  143.  
  144.                 uri = dir.resolve_relative_path(f.get_name())
  145.                 u_entry = db.entry_lookup_by_location (uri)
  146.                 if e_entry:
  147.                     u_artist, u_album = [db.entry_get (u_entry, x) for x in [rhythmdb.PROP_ARTIST, rhythmdb.PROP_ALBUM]]
  148.                     if album != u_album:
  149.                         print "Not saving local art; encountered media with different album (%s, %s, %s)" % (uri, u_artist, u_album)
  150.                         enum.close()
  151.                         return
  152.                     continue
  153.                 print "Not saving local art; encountered unknown file (%s)" % uri
  154.                 enum.close()
  155.                 return
  156.         except Exception, e:
  157.             print "Error reading \"%s\": %s" % (dir, exception)
  158.  
  159.     def save_pixbuf (self, db, entry, pixbuf):
  160.         uri = entry.get_playback_uri()
  161.         if uri is None or uri == '':
  162.             return
  163.  
  164.         f = gio.File(uri)
  165.         if uri.get_uri_scheme() == 'http':
  166.             print "not saving local art for %s" % uri
  167.             return
  168.  
  169.         print 'checking whether to save local art for %s' % uri
  170.         parent = f.get_parent()
  171.         enumfiles = parent.enumerate_children(attributes="standard::fast-content-type,access::can-read,standard::name")
  172.         enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._save_dir_cb, user_data=(db, entry, parent, pixbuf))
  173.  
  174.